package com.zpark.controller;

import cn.hutool.json.JSONUtil;
import com.zpark.common.R;
import com.zpark.entity.User;
import com.zpark.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/loginUser")
    public R loginUser(String username,String password){
        User user = userService.login(username, password);
        if(user!=null){
            return R.ok("登陆成功！").put("data",user);
        }else{
            return R.error("账号或密码错误！");
        }
    }

    @PostMapping("/addUser")
    public R addUser(@RequestBody User user){
        userService.register(user);
        if(user.getId()!=null){
            return R.ok("添加成功！");
        }else{
            return R.error("添加失败！");
        }
    }

    @PostMapping("/amendUser")
    public R amendUser(@RequestBody User user){
        userService.amendUser(user);
        return R.ok("修改成功！");
    }

    @DeleteMapping("/dropUserByIds")
    public R dropUserByIds(@RequestBody Integer[] ids){
        userService.dropUserByIds(ids);
        return R.ok("删除成功！");
    }

    @DeleteMapping("/dropUserById")
    public R dropUserByIds(Integer id){
        userService.dropUserById(id);
        return R.ok("删除成功！");
    }

    @GetMapping("/queryUserByPage")
    public R queryUserByPage(@RequestParam(value = "page",defaultValue = "1")Integer page,
                               @RequestParam(value = "limit",defaultValue = "3") Integer limit,
                               @RequestParam(value = "column",required = false) String column,
                               @RequestParam(value = "value",required = false) String value){
        int begin = (page-1)*limit + 1;
        List<User> users = userService.queryUserByPage(begin, limit, column, value);
        int count = userService.queryUserCount(column, value);
        return R.ok().put("data",users).put("count",count);
    }

    @GetMapping("/queryUserCount")
    public R queryUserCount(String column,String value){
        int count = userService.queryUserCount(column, value);
        return R.ok().put("count",count);
    }

    @GetMapping("/queryUserById")
    public R queryUserById(Integer id){
        User user = userService.queryUserById(id);
        return R.ok().put("data",user);
    }


    @RequestMapping("/queryUserByIds")
    public R queryUserByIds(@RequestBody Integer[] ids){
        List<User> users = userService.queryUserByIds(ids);
        return R.ok().put("data",users);
    }

    @GetMapping("/queryAllUser")
    public R queryAllUser(){
        List<User> users = userService.queryAllUser();
        return R.ok().put("data",users);
    }

    @GetMapping("/queryUserSexCount")
    public R queryCountBySex(){
        int women_count = userService.queryCountBySex(0);
        int man_count = userService.queryCountBySex(1);
        return R.ok().put("women_count",women_count).put("man_count",man_count);
    }

    @PostMapping("/addAllUser")
    public R addAllUser(@RequestBody String[] jsonList){
        List<User> users = new ArrayList<>();
        for (String userJson : jsonList) {
            User user = JSONUtil.toBean(userJson, User.class);
            users.add(user);
        }
        userService.addAllUser(users);
        return R.ok("导入Excel表格成功！");
    }
}
